package com.example.eduservice.controller;

import com.example.common.commonsutils.R;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.entity.vo.EduTeacherPageVO;
import com.example.eduservice.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@Tag(name = "TeacherController")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Resource
    private EduTeacherService teacherService;

    /**
     * 查找所有的老师
     *
     * @return
     */
    @Operation(description = "查询所有的讲师")
    @GetMapping("/list")
    public R findAllTeacher() {
        List<EduTeacher> teachers = teacherService.list();
        R r = R.ok().data("teachers", teachers);
        return r;
    }

    /**
     * 逻辑删除单个教师记录
     *
     * @return
     */
    @Operation(description = "逻辑删除指定id的教师")
    @DeleteMapping("{id}")
    public R removeTeacherById(@PathVariable String id) {
        boolean isRemove = teacherService.removeById(id);
        if (isRemove == false) {
            return R.error();
        }
        return R.ok();
    }

    /**
     * 组合条件查询带分页
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @param pageVO  分页条件对象
     * @return
     */
    @Operation(description = "带条件的分页查询老师")
    @PostMapping("/page/{current}/{limit}")
    public R findTeacherByConditionForPage(@PathVariable("current") Integer current,
                                           @PathVariable("limit") Integer limit,
                                           @RequestBody(required = false) EduTeacherPageVO pageVO) {
        return teacherService.findTeacherByConditionForPage(current, limit, pageVO);
    }

    @Operation(description = "新增教师")
    @PostMapping("/")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save == true) {
            return R.ok();
        }
        return R.error();
    }

    @Operation(description = "根据Id查询教师")
    @GetMapping("/{id}")
    public R findTeacherById(@PathVariable("id") String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    @Operation(description = "根据id修改教师信息")
    @PutMapping("/")
    public R modifyTeacherById(@RequestBody EduTeacher eduTeacher) {
        boolean result = teacherService.updateById(eduTeacher);
        if (result == false) {
            return R.error();
        }
        return R.ok();
    }

}
